from SysHelp import SysHelp as sh
from DrissionPage import ChromiumPage
from DrissionPage.easy_set import raise_when_ele_not_found
from DrissionPage.easy_set import set_paths
from datetime import datetime
import time
import re

sh.clear_screen()
raise_when_ele_not_found = False
page = ChromiumPage()
page.get('https://www.douyin.com/')

# 变量
t_video_info = ''

# 常量
min_stars = 3000

# 按钮
s_comment_btn = 'xpath://div[@data-e2e="feed-comment-icon"]'
s_like_btn = 'xpath://div[@data-e2e="video-player-digg"]'
s_is_digged = 'xpath://div[@data-e2e-state="video-player-is-digged"]'
s_no_digged = 'xpath://div[@data-e2e-state="video-player-no-digged"]'
s_next_btn = 'xpath://div[@data-e2e="video-switch-next-arrow"]'
s_suggest_btn = 'xpath://div[@class="Hn1a7lpc" and text="推荐"]'

# 灯片
s_active_video = 'xpath://div[@data-e2e="feed-active-video"]'
s_slide_item = 'xpath://div[@data-e2e="feed-item"]'

# 数据区
s_video_info = 'xpath://div[@id="video-info-wrap"]'

# 评论区
s_is_sideBar_show = 'xpath://div[@id="videoSideBar" and contains(@class, "dLCldFlr")]'
s_comment_list = 'xpath://div[@data-e2e="comment-list"]/div[div[@data-e2e="comment-item"]]'
s_comment_text = '.a9uirtCT'
s_comment_stars = 'xpath://div[contains(@class, "comment-item-stats-container")]/div[1]/p/span'


# 设置主标签页
if page.tabs_count == 1:
    page.set.main_tab()
    print(str(datetime.now()) + '\t设置了主标签页')
else:
    print(str(datetime.now()) + '\t有多个标签页')
    
while True:
    # 查看有几个标签
    if page.tabs_count > 1:
        page.main_tab
        page.close_other_tabs()
        print(str(datetime.now()) + '\t关闭了其它标签')
    # 找 下一个按钮, 视频窗口
    e_next_btn = page.ele(s_next_btn)
    e_active_video = page.ele(s_active_video)
    
    # 直播,跳到下一个
    if not e_active_video and e_next_btn:
        e_next_btn.click()
        print(str(datetime.now()) + '\t跳过直播,点击下一条')
        continue
    elif not e_next_btn:
        print(str(datetime.now()) + '\t现在是直播视频, 但没找到 下一个 按钮')
        e_suggest_btn = page.ele(s_suggest_btn)
        if e_suggest_btn:
            e_suggest_btn.click()
            print(str(datetime.now()) + '\t点击了打开 推荐')
            continue
        else:
            print(str(datetime.now()) + '\t也没找到 推荐 按钮,退出程序')
            break
    
    # 保存标题, 和上一个标题
    t_prev_video_info = t_video_info
    t_video_info = e_active_video.s_ele(s_video_info).text
    
    # 打开评论区
    is_sideBar_show = e_active_video.s_ele(s_is_sideBar_show)
    if is_sideBar_show:
        e_active_video.ele(s_comment_btn).click()
        print(str(datetime.now()) + '\t打开评论区')
    
    # 提取评论区信息
    best_comments = []
    # 找到已加载的评论
    e_comment_list= e_active_video.s_eles(s_comment_list)
    if e_comment_list and len(e_comment_list) > 0:
        # 循环提取
        for comment in e_comment_list:
            # 打文字和点赞数
            stars = comment.s_ele(s_comment_stars).text
            text = comment.s_ele(s_comment_text).text.replace('\n', ',')
            # 将 "万" 字替换掉
            if '万' in stars:
                stars = int(float(stars.replace('万', '')) * 10000)
            else:
                stars = int(stars)
            # 决定是否保存
            if stars > min_stars:
                best_comments.append((stars, text))
        # 打印
        if len(best_comments) > 0 and t_prev_video_info != t_video_info:
            # 点赞
            e_no_digged = e_active_video.s_ele(s_no_digged)
            if e_no_digged:
                e_active_video.ele(s_like_btn).click()
                print(str(datetime.now()) + '\t点了个赞')
                time.sleep(10)
            else:
                print(str(datetime.now()) + '\t以前点过赞了')
            # 保存
            with open('collect.txt', 'a', encoding='utf-8') as file:
                file.write('\n\n#######################################################################################################\n')
                file.write(t_video_info.replace('\n', ',') + '\n')
                file.write('----------------------------------------------------------\n')
                for (stars, text) in best_comments:
                    file.write(f'{str(stars).ljust(10)}\t{text}\n')
            # 打印提示
            print(str(datetime.now()) + '\t' + t_video_info.replace('\n', ',').ljust(38)[:40] + '...')
    # 切换下一条
    if e_next_btn:
        e_next_btn.click()
        print(str(datetime.now()) + '\t打开下一条, 一共有 ' + str(len(page.s_eles(s_slide_item))) + ' 条')
    
    # 慢一点
    time.sleep(2)
